using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Collections;
using System.Data.SqlClient;
using Microsoft.SharedSource.SqlServer.MDDE.Middleware;

namespace Microsoft.SharedSource.SqlServer.MDDE.ManagementStudio
{
    internal partial class UpgradeTemplateSelectTemplateInstances : Microsoft.SharedSource.SqlServer.MDDE.ManagementStudio.UpgradeTemplateBase
    {        
        internal UpgradeTemplateSelectTemplateInstances(UpgradeTemplateWizardController controller)
            : base(controller)
        {
            InitializeComponent();
        }

        private void UpgradeTemplateSelectTemplateInstances_Load(object sender, EventArgs e)
        {
            // Get the repository connection string and template name from controller.
            string repositoryConnectionString = controller.RepositoryConnectionString;
            string templateName = controller.TemplateName;            

            // Get the Template Instances list.
            XmlDocument templateInstances = MDDEManager.GetTemplateInstances(repositoryConnectionString, templateName);

            for (int itemCount = 0; itemCount < templateInstances.GetElementsByTagName("TemplateInstance").Count; itemCount++)
            {
                dgSelectTemplateInstance.Rows.Add(false, 
                    templateInstances.GetElementsByTagName("TemplateInstance")[itemCount].Attributes["Name"].Value,
                    templateInstances.GetElementsByTagName("TemplateInstance")[itemCount].Attributes["Name"].Value);
            }  
        }
        
        protected override void btnNext_Click(object sender, EventArgs e)
        {            
            XmlNode configurationRoot = controller.WizardConfigurations.FirstChild;

            // Locate any earlier entry for this form.
            XmlNode formSelectedTemplateInstancesInfo =
                configurationRoot.SelectSingleNode("//Form[@Name=\"SelectedTemplateInstances\"]");

            // Delete the earlier node if found.
            if (formSelectedTemplateInstancesInfo != null)
                formSelectedTemplateInstancesInfo.ParentNode.RemoveChild(formSelectedTemplateInstancesInfo);

            // Add the 'Form' element.
            formSelectedTemplateInstancesInfo =
                UIHelper.AppendChildElement(configurationRoot, "Form",
                new string[] { "Name", "SelectedTemplateInstances" });

            // Add in each selected Template Instance.            
            foreach (DataGridViewRow r0 in dgSelectTemplateInstance.Rows)
            {
                if (Convert.ToBoolean(r0.Cells["Select"].Value))
                {
                    UIHelper.AppendChildElement(formSelectedTemplateInstancesInfo, "UserInput",
                        new string[] { "SourceTemplateInstanceName", r0.Cells["Source"].Value.ToString(),
                            "NewTemplateInstanceName", r0.Cells["NewTemplateInstanceName"].Value.ToString()});
                }
            } 
            // Invoke the base method.
            base.btnNext_Click(sender, e);
        }

        private void chkSelect_Click(object sender, EventArgs e)
        {
            // Apply the selected change to each of the checkbox.
            foreach (DataGridViewRow r0 in this.dgSelectTemplateInstance.Rows)
                r0.Cells["Select"].Value = this.chkSelect.Checked;
        }

        private void dgSelectPackages_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // If the row check box is being deselected, deselect the main check box as well.
            if (e.ColumnIndex == 0)
            {
                if (chkSelect.Checked &&
                    !Convert.ToBoolean(
                    dgSelectTemplateInstance.Rows[e.RowIndex].Cells[0].EditedFormattedValue))
                    chkSelect.Checked = false;
            }
        }
        
    }
}

